{% extends "base.html" %}

{% block title %}资产管理 - 梦幻西游数字资产管理系统{% endblock %}

{% block mobile_title %}资产管理{% endblock %}

{% block content %}
<div class="container">
    <div class="row mb-4">
        <div class="col-12">
            <h2>💰 资产管理</h2>
        </div>
    </div>
    
    <!-- 资产概览 -->
    <div class="row mb-4" id="assetOverview">
        <div class="col-6 col-lg-3 mb-3">
            <div class="stat-card">
                <i class="bi bi-gem stat-icon"></i>
                <div class="stat-value" id="totalAssets">-</div>
                <div class="stat-label">总资产 (亿)</div>
            </div>
        </div>
        <div class="col-6 col-lg-3 mb-3">
            <div class="stat-card">
                <i class="bi bi-coin stat-icon"></i>
                <div class="stat-value" id="totalMoney">-</div>
                <div class="stat-label">游戏币总额 (亿)</div>
            </div>
        </div>
        <div class="col-6 col-lg-3 mb-3">
            <div class="stat-card">
                <i class="bi bi-star-fill stat-icon"></i>
                <div class="stat-value" id="totalXianyu">-</div>
                <div class="stat-label">仙玉总量</div>
            </div>
        </div>
        <div class="col-6 col-lg-3 mb-3">
            <div class="stat-card">
                <i class="bi bi-trophy stat-icon"></i>
                <div class="stat-value" id="roleCount">-</div>
                <div class="stat-label">角色数量</div>
            </div>
        </div>
    </div>
    
    <!-- 角色资产列表 -->
    <div class="card">
        <div class="card-header d-flex justify-content-between align-items-center">
            <span>各角色资产详情</span>
            <div class="btn-group btn-group-sm">
                <button class="btn btn-outline-primary active">全部</button>
                <button class="btn btn-outline-primary">按资产排序</button>
            </div>
        </div>
        <div class="card-body">
            <div class="table-responsive">
                <table class="table table-hover">
                    <thead>
                        <tr>
                            <th>角色</th>
                            <th>游戏币</th>
                            <th class="d-none d-md-table-cell">仙玉</th>
                            <th class="d-none d-md-table-cell">金币</th>
                            <th>估值</th>
                        </tr>
                    </thead>
                    <tbody id="assetTableBody">
                        <tr>
                            <td colspan="5" class="text-center py-4">
                                <div class="spinner-border text-primary" role="status">
                                    <span class="visually-hidden">加载中...</span>
                                </div>
                            </td>
                        </tr>
                    </tbody>
                </table>
            </div>
        </div>
    </div>
</div>
{% endblock %}

{% block extra_js %}
<script>
$(document).ready(function() {
    loadAssets();
});

async function loadAssets() {
    try {
        const data = await apiRequest('/api/roles');
        const roles = data.data;
        
        // 计算总资产
        let totalMoney = 0;
        let totalXianyu = 0;
        
        roles.forEach(role => {
            totalMoney += role.role_money || 0;
            totalXianyu += role.role_xianyu || 0;
        });
        
        $('#totalMoney').text((totalMoney / 100000000).toFixed(2));
        $('#totalXianyu').text(totalXianyu.toLocaleString());
        $('#totalAssets').text((totalMoney / 100000000).toFixed(2));
        $('#roleCount').text(roles.length);
        
        // 生成表格
        let html = '';
        roles.forEach(role => {
            const money = (role.role_money || 0) / 100000000;
            html += `
                <tr>
                    <td>
                        <strong>${role.role_name}</strong>
                        <br><small class="text-muted">${role.account_name}</small>
                    </td>
                    <td>${money.toFixed(2)}亿</td>
                    <td class="d-none d-md-table-cell">${(role.role_xianyu || 0).toLocaleString()}</td>
                    <td class="d-none d-md-table-cell">${((role.role_jinbi || 0) / 10000).toFixed(1)}万</td>
                    <td><strong class="text-primary">${money.toFixed(2)}亿</strong></td>
                </tr>
            `;
        });
        
        if (html === '') {
            html = `
                <tr>
                    <td colspan="5" class="text-center py-4">
                        <div class="empty-state">
                            <i class="bi bi-inbox"></i>
                            <p>暂无资产数据</p>
                        </div>
                    </td>
                </tr>
            `;
        }
        
        $('#assetTableBody').html(html);
    } catch (error) {
        console.error('加载资产数据失败:', error);
    }
}
</script>
{% endblock %}

